package com.walkline.weibo.dao;

import java.util.Vector;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import com.walkline.util.Function;
import com.walkline.weibo.WeiboException;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Favorite;
import com.walkline.weibo.inf.FavoriteWrapper;

public class WeiboFavoriteWrapper extends WeiboObject implements FavoriteWrapper
{
	private int _total_number = 0;
	private Vector _favorites = new Vector();

	public WeiboFavoriteWrapper(WeiboSDK weibo, JSONObject jsonObject) throws WeiboException
	{
		super(weibo, jsonObject);

		JSONObject favoriteWrapper = jsonObject;
		if (favoriteWrapper != null)
		{
			_total_number = favoriteWrapper.optInt("total_number");

			JSONArray favorites = favoriteWrapper.optJSONArray("favorites");
			if (favorites != null)
			{
				JSONObject favoriteObject;
				Favorite favorite;

				for (int i=0; i<favorites.length(); i++)
				{
					try {
						favoriteObject = (JSONObject) favorites.get(i);

						favorite = new WeiboFavorite(weibo, favoriteObject);
						if (favorite != null) {_favorites.addElement(favorite);}
					} catch (JSONException e) {Function.errorDialog(e.toString());}
				}
			}
		}
	}

	public int getTotalNumber() {return _total_number;}

	public Vector getFavorites() {return _favorites;}
}